package com.jay.beiange.efccp;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.widget.Toolbar;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.TextView;

import com.google.gson.Gson;
import com.jay.beiange.efccp.adapter.ProductListAdapter;
import com.jay.beiange.efccp.bean.Product;
import com.jay.beiange.efccp.contast.Constants;
import com.jay.beiange.efccp.utils.ImageLoader;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

/**
 * Created by Administrator on 2016/12/24 0024.
 */

public class ProductFragment extends Fragment {

    private Context mContext;

    private Toolbar mToolbar;
    private TextView mToolbarTitle;
    private View view;

    private String[] mListTitle;
    private String[] mListDesc;
    private Integer[] mListImgs;
    public ImageLoader imageLoader; //用来下载图片的类，后面有介绍


    ListView mListView = null;
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {

        view = inflater.inflate(R.layout.fragment_product, container, false);
        initView();
        initData();
        return view;
    }




    private void initView() {
        mToolbar = (Toolbar) view.findViewById(R.id.id_toolbar);
        mToolbarTitle = (TextView) mToolbar.findViewById(R.id.id_toolbar_title);
        mListView = (ListView) view.findViewById(R.id.id_product_list);
        mToolbarTitle.setText("产品中心");
    }

    private void initData() {



        OkHttpClient mOkHttpClient = new OkHttpClient();
        final Request request = new Request.Builder()
                .url(Constants.BASE_API_URL + "product")
                .build();
        Call call = mOkHttpClient.newCall(request);
        call.enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {

            }
            @Override
            public void onResponse(Call call, Response response) throws IOException {
                if (!response.isSuccessful()) {
                    throw new IOException("服务器端错误: " + response);
                }
                final String res = response.body().string();
                Gson gson = new Gson();
                Product product = gson.fromJson(res, Product.class);

                final List<Map<String, Object>> mData=new ArrayList<Map<String,Object>>();
                for (int i = 0; i < product.getData().size(); i++) {
                    Map<String, Object> item = new HashMap<String, Object>();
                    item.put("id",product.getData().get(i).getId());
                    item.put("image", "http://www.efccp.com/home/templates/default/images/wallpaper1_4.jpg");
                    item.put("title", product.getData().get(i).getTitle());
                    item.put("description", product.getData().get(i).getDescription());
                    mData.add(item);
                }
                getActivity().runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        ProductListAdapter adapter = new ProductListAdapter(getContext(),mData);
                        mListView.setAdapter(adapter);
                        mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                            @Override
                            public void onItemClick(AdapterView<?> adapterView, View view, int position,
                                                    long id) {
                                Intent intent;
                                intent = new Intent(getActivity(), ProductDetailActivity.class);
                                Bundle bundle = new Bundle();
                                bundle.putInt("id", (int) mData.get(position).get("id"));
                                intent.putExtras(bundle);
                                getActivity().startActivity(intent);
                            }
                        });

                    }
                });




            }


        });


    }





}
